package com.fastwire.carriere.hibernate.daoimpl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.fastwire.carriere.common.constant.AppConstants;
import com.fastwire.carriere.common.util.DateUtil;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.dao.DiscoveryRecordDao;
import com.fastwire.carriere.discovery.entity.DiscoveryRecord;
import com.fastwire.carriere.discovery.vo.DiscoveryRecordVo;

// TODO: Auto-generated Javadoc
/**
 * The Class DiscoveryRecordDaoImpl.
 */
public class DiscoveryRecordDaoImpl extends AbstractDAOImpl implements
		DiscoveryRecordDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.dao.DiscoveryRecordDao#findAllDiscoveryRecord()
	 */
	@Override
	public List findAllDiscoveryRecord() throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(DiscoveryRecord.class);
		criteria.addOrder(Order.desc("startDate"));
		return getHibernateTemplate()
				.findObjsByCriteria(criteria, false, false);

	}

	@Override
	public List findWIPDiscoverys() throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(DiscoveryRecord.class);
		criteria.add(Restrictions.eq("status",
				AppConstants.DISCOVERY_IN_PROGRESS));
		return getHibernateTemplate()
				.findObjsByCriteria(criteria, false, false);

	}

	// added by Jun on 20120425
	@Override
	public List findRadDiscovery() throws DAOException 
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(DiscoveryRecord.class);
		criteria.add(Restrictions.eq("status",	AppConstants.DISCOVERY_RAD_IN_PROGRESS));
		return getHibernateTemplate().findObjsByCriteria(criteria, false, false);
	}

	@Override
	public List<DiscoveryRecord> findDiscoveryRecordbySearchCriteria(
			DiscoveryRecordVo discoveryRecordVo) throws DAOException {

		DetachedCriteria criteria = DetachedCriteria
				.forClass(DiscoveryRecord.class);
		criteria.addOrder(Order.desc("startDate"));
		if (!StringUtil.isEmptyString(discoveryRecordVo.getStartDate())) {
			criteria.add(Restrictions.gt(
					"startDate",
					DateUtil.parseDate(DateUtil.DATE_SLASH,
							discoveryRecordVo.getStartDate())));

		}

		if (!StringUtil.isEmptyString(discoveryRecordVo.getEndDate())) {
			criteria.add(Restrictions.gt(
					"endDate",
					DateUtil.parseDate(DateUtil.DATE_SLASH,
							discoveryRecordVo.getEndDate())));

		}

		if (!StringUtil.isEmptyString(discoveryRecordVo.getType())) {
			criteria.add(Restrictions.eq("type", discoveryRecordVo.getType()));
		}

		if (!StringUtil.isEmptyString(discoveryRecordVo.getStatus())) {
			criteria.add(Restrictions.eq("status",
					discoveryRecordVo.getStatus()));
		}

		return getHibernateTemplate()
				.findObjsByCriteria(criteria, false, false);

	}

}
